#include "List.h"
#include <iostream>

List::Node* List::PushFront(void *Data)
{
	Node* Temp = new Node(Data);
	Node* Nxt = Head->Nxt;
	Temp->Nxt = Nxt; Nxt->Pre = Temp; Temp->Pre = Head; Head->Nxt = Temp;
	Size++;
	std::cout << "Successfully Insert!" << std::endl;
	return Temp;
}

List::Node* List::PushTail(void* Data)
{
	Node* Temp = new Node(Data);
	Node* Pre = _Tail->Pre;
	Pre->Nxt = Temp; Temp->Pre = Pre; Temp->Nxt = _Tail; _Tail->Pre = Temp;
	Size++;
	std::cout << "Successfully Insert!" << std::endl;
	return Temp;
}

Result List::RangFront()
{
	Node* Temp = Head->Nxt;
	if (Size == 0)
	{
		std::cout << "No Data In List!" << std::endl;
		return Wrong;
	}
	for (int i = 0; i < Size; i++)
	{
		std::cout << Temp<<std::endl;
		Temp = Temp->Nxt;
	}
	std::cout << "Done" << std::endl;
	return Result(Success);
}

Result List::RangTail()
{
	Node* Temp = _Tail->Pre;
	if (Size == 0)
	{
		std::cout << "No Data In List!" << std::endl;
		return Wrong;
	}
	for (int i = 0; i < Size; i++)
	{
		std::cout << Temp << std::endl;
		Temp = Temp->Pre;
	}
	std::cout << "Done" << std::endl;
	return Result(Success);
}

Result List::Delete(void *Data)
{
	if (Size == 0)
	{
		std::cout << "No Data In This List!" << std::endl;
		return Empty;
	}
	else
	{
		Node* Temp = Head->Nxt;
		for (int i = 0; i < Size; i++)
		{
			if (Temp->Data == Data)
			{
				Node* Pre = Temp->Pre;
				Node* Nxt = Temp->Nxt;
				Pre->Nxt = Nxt; Nxt->Pre = Pre;
				delete(Temp);
				std::cout << "Successfully Delete!" << std::endl;
				return Success;
			}
			else
			{
				Temp = Temp->Nxt;
			}
		}
	}
	std::cout << "Data Was Not Found In This List!" << std::endl;
	return Wrong;
}

Result List::PopFront()
{
	if (!Size)
	{
		std::cout << "No Data In This List!" << std::endl;
		return Empty;
	}
	else
	{
		std::cout << (Head->Nxt)->Data << std::endl<<"Done!"<<std::endl;
		return Success;
	}
}

Result List::PopTail()
{
	if (!Size)
	{
		std::cout << "No Data In This List!" << std::endl;
		return Empty;
	}
	else
	{
		std::cout << (_Tail->Pre)->Data << std::endl<<"Done!" << std::endl;
		return Success;
	}
}
